<!--#Include File="../conn.asp" -->
<!--#Include File="comm/inc.asp" -->
<%
'====================================================================
'= Team Elite - Elite Article System
'= Copyright (c) 2005 - 2006 Eason Chan All Rights Reserved.
'=-------------------------------------------------------------------
'= 版权协议：
'=	GPL (The GNU GENERAL PUBLIC LICENSE Version 2, June 1991)
'=-------------------------------------------------------------------
'= 文件名称：/Manager/Admin_Column.asp
'= 摘    要：后台-栏目管理文件
'=-------------------------------------------------------------------
'= 最后更新：eason007
'= 最后日期：2005-09-30
'====================================================================

Call EA_Manager.Chk_IsMaster

If Not EA_Manager.Chk_Power(Admin_Power,"11") Then 
	ErrMsg=str_Comm_NotAccess
	Call EA_Manager.Error(1)
End If

Dim Atcion
Atcion=Request.QueryString ("atcion")

Select Case LCase(Atcion)
Case "save"
	Call Save
Case "del"
	Call del
Case "add"
	Call Add
Case "up"
	Call MoveColumn(1)
Case "down"
	Call MoveColumn(0)
Case "reset"
	Call ResetAllBoard()
Case Else
	Call Main
End Select
Call EA_Pub.Close_Obj
Set EA_Pub=Nothing

Sub Main
	Dim Level,i
	Dim List,ColumnRetract
	Dim Temp,ListBlock

	PageContent=Template.LoadTemplate("admin_column_list.htm")


	Template.SetVariable "Language_Comm_Bar_Operation",str_Comm_Bar_Operation,PageContent
	Template.SetVariable "Language_Comm_Edit_Operation",str_Comm_Edit_Operation,PageContent
	Template.SetVariable "Language_Comm_Alert_Del_Operation",str_Comm_Alert_Del_Operation,PageContent
	Template.SetVariable "Language_Comm_Del_Operation",str_Comm_Del_Operation,PageContent

	Template.SetVariable "Language_OperationNotice",str_OperationNotice,PageContent
	Template.SetVariable "Language_Column_Help",str_Column_Help,PageContent

	Template.SetVariable "Language_Column_ColumnList",str_Column_ColumnList,PageContent
	Template.SetVariable "Language_Column_AddColumn",str_Column_AddColumn,PageContent
	Template.SetVariable "Language_Column_Title",str_Column_Title,PageContent
	Template.SetVariable "Language_Column_ManagerArticleTotal",str_Column_ManagerArticleTotal,PageContent
	Template.SetVariable "Language_Column_ArticleTotal",str_Column_ArticleTotal,PageContent
	Template.SetVariable "Language_Column_Order",str_Column_Order,PageContent
	Template.SetVariable "Language_Column_Confirm_ResetBoard",str_Column_Confirm_ResetBoard,PageContent
	Template.SetVariable "Language_Column_ResetBoard",str_Column_ResetBoard,PageContent
	Template.SetVariable "Language_Column_MoveArticle",str_Column_MoveArticle,PageContent
	Template.SetVariable "Language_Column_DelArticle",str_Column_DelArticle,PageContent


	ListBlock=Template.GetBlock("list",PageContent)

	List=EA_DBO.Get_Column_List()
    If IsArray(List) Then 
		For i=0 To UBound(List,2)
			Temp=ListBlock
			ColumnRetract = ""

			Template.SetVariable "ColumnID",List(0,i),Temp
			Template.SetVariable "ColumnURL",EA_Pub.Cov_ColumnPath(List(0,i),EA_Pub.SysInfo(18),List(2,i)),Temp
			Template.SetVariable "ColumnName",List(1,i),Temp

			Level=(Len(List(2,i))/4-1)*4
			If Len(List(2,i))>4 Then ColumnRetract = ColumnRetract & "├"
			ColumnRetract = ColumnRetract & String(Level,"-")
			Template.SetVariable "ColumnRetract",ColumnRetract,Temp

			If List(5,i)>0 Then
				Template.SetVariable "ColumnManagerArticleTotal","<font color=red><b>" & List(5,i) & "</b></font>",Temp
			Else
				Template.SetVariable "ColumnManagerArticleTotal",List(5,i),Temp
			End If

			Template.SetVariable "IsNav",List(6,i),Temp
			Template.SetVariable "IsDis",List(7,i),Temp
			Template.SetVariable "ColumnArticleTotal",List(4,i),Temp
			

			Template.SetBlock "list",Temp,PageContent
		Next
	End If
	Template.CloseBlock "list",PageContent

	Template.BaseReplace PageContent
	Template.OutStr PageContent
End Sub

Sub Add
	Dim PostId
	Dim i,Level
	Dim Temp,ColumnList,ListTemplateList,ArticleTemplateList
	Dim Name,Code,Typer,Power,Info,IsHide,OutUrl,Style,IsReview,IsPost,IsTop,List_TempId,Article_TempId,PageSize,ShowUrl
	
	PostId=EA_Pub.SafeRequest(3,"postid",0,0,0)
	Power=0
	IsReview=-1
	IsPost=-1
	IsTop=-1
	PageSize=10
	Style=0
	Code="0000"
	
	Temp=EA_DBO.Get_Column_Info(PostId)
	If IsArray(Temp) Then 
		Name=Temp(0,i)
		Code=Temp(1,i)
		Typer=Temp(11,i)
		Power=Temp(12,i)
		Info=Temp(2,i)
		IsHide=Temp(13,i)
		OutUrl=Temp(7,i)
		Style=Temp(8,i)
		IsReview=Temp(14,i)
		IsPost=Temp(15,i)
		IsTop=Temp(16,i)
		List_TempId=Temp(9,i)
		Article_TempId=Temp(10,i)
		PageSize=Temp(17,i)
		ShowUrl = Temp(18,i)
	End If

	Temp=EA_DBO.Get_Column_List()
	If IsArray(Temp) Then
		For i=0 To UBound(Temp,2)
			Level=(Len(Temp(2,i))/4-1)*3
			ColumnList = ColumnList & "<option value="""&Temp(2,i)&""""
			If Code<>"" Then 
				If Left(Code,Len(Code)-4)=Temp(2,i) Then ColumnList = ColumnList & " selected"
			End If
			ColumnList = ColumnList & ">"
			ColumnList = ColumnList & "├"
			ColumnList = ColumnList & String(Level,"-")
			ColumnList = ColumnList & Temp(1,i)&"</option>"
		Next
	End If

	Temp=EA_M_DBO.Get_Template_List()
	If IsArray(Temp) Then
		For i=0 To UBound(Temp,2)
			ListTemplateList = ListTemplateList & "<option value="""&Temp(0,i)&""""
			If CLng(List_TempId)=CLng(Temp(0,i)) Then ListTemplateList = ListTemplateList & " selected"
			ListTemplateList = ListTemplateList & ">"
			ListTemplateList = ListTemplateList & Temp(1,i)&"</option>"
		Next
	End If

	Temp=EA_M_DBO.Get_Template_List()
	If IsArray(Temp) Then
		For i=0 To UBound(Temp,2)
			ArticleTemplateList = ArticleTemplateList & "<option value="""&Temp(0,i)&""""
			If CLng(Article_TempId)=CLng(Temp(0,i)) Then ArticleTemplateList = ArticleTemplateList & " selected"
			ArticleTemplateList = ArticleTemplateList & ">"
			ArticleTemplateList = ArticleTemplateList & Temp(1,i)&"</option>"
		Next
	End If

	PageContent=Template.LoadTemplate("admin_column_option.htm")


	Template.SetVariable "Language_Comm_Save_Button",str_Comm_Save_Button,PageContent
	Template.SetVariable "Language_Comm_Reset_Button",str_Comm_Reset_Button,PageContent
	Template.SetVariable "Language_Comm_Yes",str_Comm_Yes,PageContent
	Template.SetVariable "Language_Comm_No",str_Comm_No,PageContent
	Template.SetVariable "Language_Comm_Reduce_Input",str_Comm_Reduce_Input,PageContent
	Template.SetVariable "Language_Comm_Zoom_Input",str_Comm_Zoom_Input,PageContent
	Template.SetVariable "Language_Comm_Select",str_Comm_Select,PageContent

	Template.SetVariable "Language_OperationNotice",str_OperationNotice,PageContent
	Template.SetVariable "Language_Column_Help",str_Column_Help,PageContent

	Template.SetVariable "Language_Column_ColumnList",str_Column_ColumnList,PageContent
	'add by jecray
	Template.SetVariable "Language_Column_ShowUrl",str_Column_ShowUrl,PageContent
	Template.SetVariable "Language_Column_Root",str_Column_Root,PageContent
	
	
	Template.SetVariable "Language_Column_AddColumn",str_Column_AddColumn,PageContent
	Template.SetVariable "Language_Column_Input_Column",str_Column_Input_Column,PageContent
	Template.SetVariable "Language_Column_Title",str_Column_Title,PageContent
	Template.SetVariable "Language_Column_Type",str_Column_Type,PageContent
	Template.SetVariable "Language_Column_Attrib",str_Column_Attrib,PageContent
	Template.SetVariable "Language_Column_Info",str_Column_Info,PageContent
	Template.SetVariable "Language_Column_OutURL",str_Column_OutURL,PageContent
	Template.SetVariable "Language_Column_ViewPower",str_Column_ViewPower,PageContent
	Template.SetVariable "Language_Column_IsHide",str_Column_IsHide,PageContent
	Template.SetVariable "Language_Column_IsReview",str_Column_IsReview,PageContent
	Template.SetVariable "Language_Column_IsPost",str_Column_IsPost,PageContent
	Template.SetVariable "Language_Column_IsTop",str_Column_IsTop,PageContent
	Template.SetVariable "Language_Column_Style",str_Column_Style,PageContent
	Template.SetVariable "Language_Column_ListTemplate",str_Column_ListTemplate,PageContent
	Template.SetVariable "Language_Column_ArticleTemplate",str_Column_ArticleTemplate,PageContent
	Template.SetVariable "Language_Column_PageSize",str_Column_PageSize,PageContent
	Template.SetVariable "Language_Column_Type_Normal",str_Column_Type_Normal,PageContent
	Template.SetVariable "Language_Column_Type_Diss",str_Column_Type_Diss,PageContent
	Template.SetVariable "Language_Column_Style_Text",str_Column_Style_Txt,PageContent
	Template.SetVariable "Language_Column_Style_Info",str_Column_Style_Info,PageContent
	Template.SetVariable "Language_Column_Style_Img",str_Column_Style_Img,PageContent
	Template.SetVariable "Language_Column_Style_Txt_Info_Img",str_Column_Style_Txt_Info_Img,PageContent

	Template.SetVariable "ColumnList",ColumnList,PageContent
	Template.SetVariable "ListTemplateList",ListTemplateList,PageContent
	Template.SetVariable "ArticleTemplateList",ArticleTemplateList,PageContent

	Template.SetVariable "ColumnID",PostId,PageContent
	Template.SetVariable "ColumnTitle",Name,PageContent
	Template.SetVariable "Column_Type_" & Typer," selected",PageContent
	If Left(Code,Len(Code)-4)="" Then Template.SetVariable "ColumnAttrib"," selected",PageContent
	Template.SetVariable "ColumnInfo",Info,PageContent
	Template.SetVariable "ColumnOutURL",OutURL,PageContent
	Template.SetVariable "ColumnViewPower",Power,PageContent
	Template.SetVariable "Column_IsHide_" & Abs(IsHide)," checked",PageContent
	Template.SetVariable "Column_IsReview_" & Abs(IsReview)," checked",PageContent
	Template.SetVariable "Column_IsPost_" & Abs(IsPost)," checked",PageContent
	Template.SetVariable "Column_IsTop_" & Abs(IsTop)," checked",PageContent
	Template.SetVariable "Column_Style_" & Style," selected",PageContent
	Template.SetVariable "ColumnPageSize",PageSize,PageContent
	'add by jecray
	Template.SetVariable "ColumnShowUrl",ShowUrl,PageContent
	

	Template.BaseReplace PageContent
	Template.OutStr PageContent
End Sub

Sub ResetAllBoard()
	Call EA_Pub.Chk_Post
	
	Dim i,Temp
	i=1
	
	Temp=EA_DBO.Get_Column_List()
	
	If IsArray(Temp) Then
		For i=0 To UBound(Temp,2)
			SQL="UpDate [NB_Column] Set Code='"&Right("0000"&(i+1),4)&"' Where [Id]="&Temp(0,i)
			Conn.Execute(SQL)
		Next
	End If
	
	Call EA_Pub.Close_Obj
	Set EA_Pub=Nothing
	
	Response.Redirect Request.ServerVariables("HTTP_REFERER")	
End Sub

Sub MoveColumn(IsUp)
	On Error Resume Next
	Err.Clear
	Call EA_Pub.Chk_Post
	
	Dim PostId
	Dim Temp
	Dim ColumnCode,TempStr,CodeLen,NBStr,WStr

	PostId=EA_Pub.SafeRequest(3,"postid",0,0,0)
	
	Temp=EA_DBO.Get_Column_Info(PostId)
	If IsArray(Temp) Then 
		ColumnCode=Temp(1,0)
		CodeLen=Len(ColumnCode)
		NBStr=String(CodeLen,"-")
	End If
	
	If ColumnCode<>"" And (Len(ColumnCode) Mod 4)=0 Then 
		If CodeLen>4 Then WStr="and left(code,"&CodeLen-4&")='"&Left(ColumnCode,CodeLen-4)&"'"
		If IsUp Then 
			SQL="select top 1 code from [NB_Column] where len(code)="&CodeLen&" and code<'"&ColumnCode&"' "&WStr&" order by code desc"
			TempStr=conn.execute(sql)(0)
		Else
			SQL="select top 1 code from [NB_Column] where len(code)="&CodeLen&" and code>'"&ColumnCode&"' "&WStr&" order by code"
			TempStr=conn.execute(sql)(0)
		End If

		If Err Then 
			ErrMsg=str_Column_EofOrBof
			Call EA_Manager.Error(1)
		End If
			
		'Move Under Column
		If iDataBaseType = 0 Then
			SQL="update nb_column set code='"&NBStr&"'+mid(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&TempStr&"'"
		Else
			SQL="update nb_column set code='"&NBStr&"'+SUBSTRING(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&TempStr&"'"
		End If
		Conn.Execute(SQL)

		'Update Target Column
		If iDataBaseType = 0 Then
			SQL="update nb_column set code='"&TempStr&"'+mid(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&ColumnCode&"'"
		Else
			SQL="update nb_column set code='"&TempStr&"'+SUBSTRING(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&ColumnCode&"'"
		End If
		Conn.Execute(SQL)

		'Update Under Column
		If iDataBaseType = 0 Then
			SQL="update nb_column set code='"&ColumnCode&"'+mid(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&NBStr&"'"
		Else
			SQL="update nb_column set code='"&ColumnCode&"'+SUBSTRING(code,"&CodeLen+1&",len(code)) where left(code,"&CodeLen&")='"&NBStr&"'"
		End If
		Conn.Execute(SQL)

		If iDataBaseType<>2 Then
			'Update Table Of Article's Column Information
			SQL="update [NB_Content] left join [NB_Column] c on [NB_Content].columnid=c.id set columncode=c.code,columnname=c.title where columncode like '"&ColumnCode&"%'"
			Conn.Execute(SQL)
		End If
	End If
	
	Call EA_Pub.Close_Obj
	Set EA_Pub=Nothing
	
	Response.Redirect Request.ServerVariables("HTTP_REFERER")
End Sub

Sub Save
	Call EA_Pub.Chk_Post
	
	Dim Name,Power,Info,IsHide,Typer,IsOut,OutUrl,Style,IsReview,IsPost,IsTop,List_TempId,Article_TempId,PageSize,ShowUrl
	Dim MatchStr,ParentCode,TypeCode,SelfCode,StepLeng
	Dim EditCode,SourCode
	Dim PostId
	
	EditCode=False
	StepLeng=4
	MatchStr=String(StepLeng,"____")
	
	PostId=EA_Pub.SafeRequest(3,"postid",0,0,0)
	Name=EA_Pub.SafeRequest(2,"name",1,"",1)
	Power=EA_Pub.SafeRequest(2,"power",0,0,0)
	Info=EA_Pub.SafeRequest(2,"info",1,"",0)
	IsHide=EA_Pub.SafeRequest(2,"ishide",0,0,0)
	Typer=EA_Pub.SafeRequest(2,"type",0,0,0)
	OutUrl=EA_Pub.SafeRequest(2,"url",1,"",0)
	ParentCode=EA_Pub.SafeRequest(2,"code",1,"",0)
	Style=EA_Pub.SafeRequest(2,"style",0,0,0)
	IsReview=EA_Pub.SafeRequest(2,"isreview",0,1,0)
	IsPost=EA_Pub.SafeRequest(2,"ispost",0,1,0)
	IsTop=EA_Pub.SafeRequest(2,"istop",0,1,0)
	List_TempId=EA_Pub.SafeRequest(2,"list_temp",0,1,0)
	Article_TempId=EA_Pub.SafeRequest(2,"article_temp",0,1,0)
	PageSize=EA_Pub.SafeRequest(2,"pagesize",0,15,0)
	ShowUrl=EA_Pub.SafeRequest(2,"ShowUrl",1,"",1)
	
	ParentCode=CStr(ParentCode)
	
	If Name="" Or Len(Name)>50 Then 
		ErrMsg="栏目名称为空或长度超过50个字符！"
		Call EA_Manager.Error(1)
	End If
	
	If ShowUrl="" Or Len(ShowUrl)>50 Then 
		ErrMsg="栏目显示URL为空或长度超过50个字符！"
		Call EA_Manager.Error(1)
	End If
	
	If Len(OutUrl)>0 Then
		IsOut=1
	Else
		IsOut=0
	End If
		
	If PostId<>0 Then
		SourCode=EA_DBO.Get_Column_Info(PostId)(1,0)
		SourCode=CStr(SourCode)
		If ParentCode<>CStr(Left(SourCode,Len(SourCode)-4)) Then 
			If ParentCode<>SourCode Then 
				If Left(ParentCode,Len(SourCode))<>SourCode Then EditCode=True
			End If
		End If
	Else
		EditCode=True
	End If
	
	If EditCode Then 
		Sql="Select Top 1 Code from [NB_Column] Where Code Like '"&ParentCode&MatchStr&"' Order By Code Desc"
		Set Rs=Conn.Execute(SQL)
		If rs.eof Then
			TypeCode=ParentCode&String(StepLeng-1,"0")&"1"
		Else
			SelfCode=Int(Right(rs(0),StepLeng))+1
			SelfCode=Right(String(StepLeng-1,"0")&SelfCode,StepLeng)
			TypeCode=ParentCode&SelfCode
		End If
		Rs.Close
	End If
	
	If PostId=0 Then 
		EA_DBO.Set_System_ColumnTotal 1
			
		Sql="INSERT INTO NB_Column ( Title, Code, Info, Type, IsOut, OutUrl, StyleId, IsReview, IsPost, IsTop, List_TempId, Article_TempId, PageSize, ListPower, IsHide ,ShowUrl)"
		Sql=Sql&" VALUES ( '"&Name&"','"&TypeCode&"','"&Info&"',"&Typer&","&IsOut&",'"&OutUrl&"',"&Style&","&IsReview&","&IsPost&","&IsTop&","&List_TempId&","&Article_TempId&","&PageSize&","&Power&","&IsHide&",'"&ShowUrl&"')"
	ElseIf IsNumeric(PostId) And PostId<>"" And PostId<>"0" Then
		Sql="Update [NB_Column] Set Title='"&Name&"',Info='"&Info&"',Type="&Typer&",IsOut="&IsOut&",OutUrl='"&OutUrl&"',StyleId="&Style&",IsReview="&IsReview&",IsPost="&IsPost&",IsTop="&IsTop&",List_TempId="&List_TempId&",Article_TempId="&Article_TempId&",PageSize="&PageSize&",ListPower="&Power&",IsHide="&IsHide&",ShowUrl='"&ShowUrl&"'"
		If EditCode Then Sql=Sql&",Code='"&TypeCode&"'"
		Sql=Sql&" Where Id="&PostId
	End If
	Conn.Execute(SQL)

	If EditCode And PostId<>0 Then 
		SQL="update [NB_Column] Set Code='"&TypeCode&"'+Right(Code,Len(Code)-"&Len(SourCode)&") Where Code Like '"&SourCode&"%'"
		Conn.Execute(SQL)
	End If
		
	Set Rs=Nothing
	
	Call EA_Pub.Close_Obj
	Set EA_Pub=Nothing
	
	Response.Redirect "admin_column.asp"
End Sub

Sub Del
	Call EA_Pub.Chk_Post
	
	Dim PostId
	PostId=EA_Pub.SafeRequest(3,"postid",0,0,0)
	
	Dim DelBool,TxtCount,Under
	DelBool=False
		
	TxtCount=EA_M_DBO.Get_Column_ArticleTotal(PostId)(0,0)
	If TxtCount>0 Then DelBool=True:ErrMsg=str_Column_ColumnIsNotEmpty
		
	Sql="Select Count(Id) From [NB_Column] Where Code Like (Select Code From [NB_Column] Where Id="&PostId&")+'%' And Id<>"&PostId
	Under=Conn.Execute(SQL)(0)
	If Under>0 Then DelBool=True:ErrMsg=str_Column_ColumnHaveUnder
		
	If Not DelBool Then 
		EA_M_DBO.Set_Column_Delete PostId
		EA_DBO.Set_System_ColumnTotal -1

		Application.Lock 
		Application(sCacheName&"IsFlush")=1
		Application.UnLock 
	Else
		Call EA_Manager.Error(1)
	End If

	Call EA_Pub.Close_Obj
	Set EA_Pub=Nothing
	
	Response.Redirect Request.ServerVariables("HTTP_REFERER")
End Sub
%>